package com.nlx.notes.module.controller;

import com.nlx.notes.module.bean.dto.category.CategoryDeleteDTO;
import com.nlx.notes.module.bean.dto.category.CategorySaveDTO;
import com.nlx.notes.module.bean.dto.category.CategoryUpdateDTO;
import com.nlx.notes.module.bean.vo.CategoryVO;
import com.nlx.notes.module.core.util.JSONUtils;
import com.nlx.notes.module.service.ICategoriesService;
import com.nlx.notes.support.context.UserHolder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author nlx
 * @since 2024-12-15
 */
@Slf4j
@RestController
@RequestMapping("/categories")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class CategoriesController {

    final ICategoriesService categoriesService;

    /**
     * 分类列表
     * @return  s
     */
    @GetMapping(value = "/category-list")
    public List<CategoryVO> categoryList(@RequestParam(value = "categoryType",required = false) String categoryType){
        log.info("查询用户的分类列表：{},{}", UserHolder.getUserContext().getId(),categoryType);
        return categoriesService.categoryList(categoryType,UserHolder.getUserContext().getId());
    }

    @PostMapping(value = "/add")
    public CategoryVO addCategory(@RequestBody CategorySaveDTO categorySaveDTO){
        log.info("新增分类，用户：{},，参数：{}", UserHolder.getUserContext().getId(), JSONUtils.toString(categorySaveDTO));
        return categoriesService.addCategory(UserHolder.getUserContext().getId(),categorySaveDTO);
    }


    @PostMapping(value = "/update")
    public List<CategoryVO> updateCategory(@RequestBody CategoryUpdateDTO categoryUpdateDTO){
        log.info("修改分类，用户：{},，参数：{}", UserHolder.getUserContext().getId(), JSONUtils.toString(categoryUpdateDTO));
        return categoriesService.updateCategory(UserHolder.getUserContext().getId(),categoryUpdateDTO);
    }

    @PostMapping(value = "/delete")
    public List<CategoryVO> deleteCategory(@RequestBody CategoryDeleteDTO categoryDeleteDTO){
        log.info("删除分类，用户：{},，参数：{}", UserHolder.getUserContext().getId(), JSONUtils.toString(categoryDeleteDTO));
        return categoriesService.deleteCategory(UserHolder.getUserContext().getId(),categoryDeleteDTO);
    }






}
